package ATM;

public class Withdrawal extends Transaction implements ExecuteTransaction
{
// represents an ATM withdrawal transaction
	
	private int amount; // amount to withdraw
	private Keypad keypad;
	private CashDispenser cashDispenser;
		
	public Withdrawal(int userAccountNumber, Screen atmScreen, BankDatabase atmBankDatabase,
			Keypad atmKeypad, CashDispenser atmCashDispenser)
	{
		
		super(userAccountNumber, atmScreen, atmBankDatabase);
		
		keypad = atmKeypad;
		cashDispenser = atmCashDispenser;		
	}
	
	@Override
	public void execute()
	{
		boolean cashDispensed = false;
		double availableBalance;
		
		BankDatabase bankDatabase = getBankDatabase(); //you need the database object in order to access the Account class members
		Screen screen = getScreen(); // the Screen object is needed to access specific displaying methods 
		
		do
		{
			amount = displayMenuOfAmounts();
			
			if(amount != 6)
			{
				availableBalance = bankDatabase.getAvailableBalance(getAccountNumber());
				
				if(amount <= availableBalance) // checks whether the user has sufficient funds in his account
				{ 
					if(cashDispenser.isSufficientCashAvailable(amount)) // checks whether the ATM has sufficient funds to give to the user
					{ 
						bankDatabase.debit(getAccountNumber(), amount);
						
						cashDispenser.dispenseCash(amount);
						cashDispensed = true;
						
						screen.displayMessageLine("\nYour cash has been dispensed. Please take your cash now.");
					}
					else
						screen.displayMessageLine("\nInsufficient cash available in the ATM." +
								"\n\nPlease choose a smaller amount.");
				}
				else
				{
					screen.displayMessageLine("\nInsufficient funds in your account." +
							"\n\nPlease choose a smaller amount.");
				}
			}
			else
			{
				screen.displayMessageLine("\nCanceling transaction...");
				return;
			}
		}while(!cashDispensed);
	}
	
	private int displayMenuOfAmounts()
	{
		int userChoice = 0;
		Screen screen = getScreen();
		int[] amounts = { 0, 20, 40, 60, 100, 200};
		
		while(userChoice == 0)
		{
			screen.displayMessageLine("\nWithdrawal Menu: "); //the user can whitdraw money in units of: 20$, 40$, 60$, 100$ and 200$ bills
			screen.displayMessageLine("1 - $20");
			screen.displayMessageLine("2 - $40");
			screen.displayMessageLine("3 - $60");
			screen.displayMessageLine("4 - $100");
			screen.displayMessageLine("5 - $200");
			screen.displayMessageLine("6 - Cancel transaction");
			screen.displayMessage("\nChoose a withdrawal amount: ");
			
			int input = keypad.getInput();
				
			switch(input)
			{
				case 1:
				case 2:
				case 3:
				case 4:
				case 5:
					userChoice = amounts[input];
					break;
				case 6:
					userChoice = 6;
					break;
				default:
					screen.displayMessageLine("\nInvalid selection. Try again.");
			}
		}
	
	return userChoice; //the userChoice is needed in the upper execute() method in order to know the amount to be extracted
	}
}